package com.ruoyi.hk.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.hk.domain.DoctorRegist;
import com.ruoyi.hk.domain.HSysUser;
import com.ruoyi.hk.domain.User;
import com.ruoyi.hk.domain.dto.UserDto;
import com.ruoyi.hk.hanler.enums.BusinessCode;
import com.ruoyi.hk.hanler.exception.Assert;
import com.ruoyi.hk.mapper.UserMapper;
import com.ruoyi.hk.service.DoctorRegistService;
import com.ruoyi.hk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DoctorRegistService doctorRegistService;
    @Override
    @Transactional
    public Integer addUser(UserDto userDto) {
        String userType = userDto.getUserType();
        Integer phoneCount = userMapper.getUserPhone(userDto.getPhonenumber());
        Integer nicknameCount = userMapper.getNickName(userDto.getNickName());
        Integer usernameCount = userMapper.getUserName(userDto.getUserName());
        
        // 所有用户类型都需要检查手机号和昵称和用户账号重复性
        Assert.error(phoneCount != 0, BusinessCode.PHONENUMBER_ERROR);
        Assert.error(nicknameCount != 0, BusinessCode.NICKNAME_ERROR);
        Assert.error(usernameCount != 0, BusinessCode.USERNAME_ERROR);

        User user = new User();

        // 如果是医生类型，设置部门信息
        if (userType.equals("102")) {
            user.setUserName(userDto.getUserName());
            user.setNickName(userDto.getNickName());
            user.setPhonenumber(userDto.getPhonenumber());
            user.setUserType(userDto.getUserType());
            user.setCreateBy(userDto.getUserName());
            user.setSex(userDto.getSex());
            user.setUserAge(userDto.getUserAge());
            user.setAvatar("医生默认头像.png");
            user.setEmail(userDto.getUserName() + "@qq.com");
            // 对密码进行加密处理
            user.setPassword(SecurityUtils.encryptPassword(userDto.getPassword()));
            user.setCreateTime(new Date());
            user.setUpdateBy(userDto.getUserName());
            user.setUpdateTime(new Date());
            user.setStatus('0');
            user.setDepartmentTypeId(userDto.getDepartmentTypeId());

            //给注册表添加数据
            DoctorRegist doctorRegist = new DoctorRegist();
            doctorRegist.setDoctorName(userDto.getUserName());
            doctorRegist.setNickName(userDto.getNickName());
            doctorRegist.setPassword(SecurityUtils.encryptPassword(userDto.getPassword()));
            doctorRegist.setDepartmentId(userDto.getDepartmentTypeId());
            doctorRegist.setCreateTime(new Date());
            doctorRegist.setAuditState(1);

            Integer result = userMapper.addUser(user);
            doctorRegistService.addDoctorRegist(doctorRegist);
            return result;
        }
            user.setUserName(userDto.getUserName());
            user.setNickName(userDto.getNickName());
            user.setPhonenumber(userDto.getPhonenumber());
            user.setUserType(userDto.getUserType());
            user.setCreateBy(userDto.getUserName());
            user.setSex(userDto.getSex());
            user.setUserAge(userDto.getUserAge());
            user.setEmail(userDto.getUserName() + "@qq.com");
            // 对密码进行加密处理
            user.setPassword(SecurityUtils.encryptPassword(userDto.getPassword()));
            user.setCreateTime(new Date());
            user.setUpdateBy(userDto.getUserName());
            user.setUpdateTime(new Date());
            user.setStatus('0');
        // 插入用户并获取生成的ID
        Integer result = userMapper.addUser(user);

        // 获取新插入用户的ID
        Long userId = user.getUserId();

        // 调用添加用户角色的方法
        if (userId != null) {
            userMapper.addUserRole(userId, userDto.getUserType());
        }
        
        return result;
    }

    @Override
    public Integer updateDoctorQX(HSysUser user) {
        Long userId = user.getUserId();
        String userType = user.getUserType();
        return userMapper.addUserRole(userId, userType);
    }

    @Override
    public User getById(Long userId) {
        User user = userMapper.getById(userId);
        return user;
    }

    @Override
    public User selectById(Long otherUserId) {
        return userMapper.selectById(otherUserId);
    }

    @Override
    public List<User> getUserAll(Map<String, Object> params) {
        return userMapper.getUserAll(params);
    }
}
